<template>
  <div style="flex: 1;">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:cri>
        <div novalidate  partial >
        </div>
      </criteria>
      <data-grid :model="model"   partial='list' v-ref:grid>
        <template partial='head'>
          <tr >
            <th><nobr>序号</nobr></th>
            <th v-for = "row in $parent.$parent.$parent.replacejson"><nobr>{{row}}</nobr></th>
            <!-- <th v-for = "row in model.rows[$index]"><nobr>{{$parent.$parent.$parent.$parent.replacejson[$key]}}</nobr></th> -->
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td>{{$index+1}}</td>
            <th v-for = "row in $parent.$parent.$parent.replacejson"><nobr>
              <span>{{model.rows[$parent.$index][$key]}}</span>
            </nobr></th>
            <!-- <td v-for = "row in model.rows[$index]"><nobr>{{row}}</nobr></td> -->
          </tr>
        </template>
        <template partial='foot'>

        </template>
      </data-grid>
    </criteria-paged>
  </div>

</template>
<script>
import { PagedList } from 'vue-client'
import getjsondata from '../ComprehenQuery/utils/jsonData'

export default {
  title: '查询结果显示',
  props: {
    tablename: {
      type: String,
      default: 't_userinfo'
    },
    condition: {
      type: String,
      default: '1=1'
    },
    conditions: {
      type: Object,
      default: {}
    },
    tabactive: {
      type: Object,
      default: {}
    },
    replacejson: {
      type: Object,
      default: {}
    },
    usercondition: {
      type: Object,
      default: {}
    }
  },
  data () {
    return {
      model: new PagedList('rs/sql/singleTable_OrderBy', 20,
       {items: "'*'", tablename: "'t_userinfo'", condition: "'1=1'", orderitem: "'f_userinfo_id DESC'"})
    }
  },
  ready () {
    // this.replacejson = getjsondata.data['userinfo']['json']
    // this.$refs.paged.$refs.cri.search()
    this.$dispatch('default-search')
  },
  methods: {
    // 发送请求
    getdata (url, data, sums) {
      if (sums) {
        this.model = new PagedList(url, 20,
          {items: '"' + data.items + '"', tablename: '"' + data.tablename + '"', condition: '"' + data.condition + '"', orderitem: "'" + data.orderitem + "'", matchitem: "'" + data.matchitem + "'"},
          sums)
      } else {
        this.model = new PagedList(url, 20,
          {items: '"' + data.items + '"', tablename: '"' + data.tablename + '"', condition: '"' + data.condition + '"', orderitem: "'" + data.orderitem + "'", matchitem: "'" + data.matchitem + "'"})
      }
      // this.$info(`整理数据，发送分页请求，请求地址: ${url},所带参数tablename: ${tablename},condition: ${condition}, orderitem:${orderitem}`)
      this.model.search()
    },
    // 连表查询 添加前缀
    sortitems (nameval) {
      let items = ''
      for (let name in getjsondata.data[nameval]['json']) {
        items += ' t.' + name + ','
      }
      items = items.substr(0, items.length - 1)
      items += ' '
      return items
    },
    inquiredata (ExportEX) {
      let tab = this.tabactive
      for (let key in tab) {
        if (tab[key] === true) {
          this.$info(`切换到标签: ${key.split('__')[0]}`)
          let name = key.split('__')[0]
          // 获取表名
          let tablename = 't_' + key.split('__')[0]
          // 整理条件
          if (!this.condition) {
            this.condition = '1=1'
          }
          // 获取需要查询的列名
          this.replacejson = getjsondata.data[name]['json']
          // 整理查询的列名
          let item = this.sortitems(name)
          this.$info(`查询的列为: ${item}`)
          // 获取排序字段
          let orderitem = getjsondata.data[name]['orderitem']
          // 获取，整理匹配条件
          let matchitem = getjsondata.data[name]['matchitem']
          // 获取合计项目
          let sums = getjsondata.data[name]['sums']
          // 获取sql 地址
          let url
          if (getjsondata.data[name]['url'] && getjsondata.data[name]['url'] !== '') {
            url = getjsondata.data[name]['url']
          } else {
            url = 'rs/sql/singleTable_Public'
          }
          // 发送请求
          let data = {
            items: item,
            tablename: tablename,
            condition: this.condition,
            orderitem: orderitem,
            matchitem: matchitem
          }
          // 若有传值为true  则不发请求 返回相应数据
          if (ExportEX === true) {
            return data
          }
          this.getdata(url, data, sums)
        }
      }
    }
  },
  computed: {
  },
  watch: {
    'condition': {
      deep: true,
      handler (val) {

      }
    },
    'tabactive': {
      deep: true,
      handler (val) {

      }
    }
  }
}
</script>
